local socket = require("socket")
local function GetAdd(hostname)
    local ip, resolved = socket.dns.toip(hostname)
    local ListTab = {}
    for k, v in ipairs(resolved.ip) do
        table.insert(ListTab, v)
    end
    return ListTab
end
local hostname = GetAdd(socket.dns.gethostname())

function net:serverInit()
    local host = hostname[2] or hostname[1] or "localhost"
    self.server = sock.newServer(host,22122,10)
    print("server running on "..host,22122)
    self.clients = {}
    self.rooms = {}
    self.server:on("connect", function(data,client)
        print("client:"..client.connectId.." connected")
    end)

    self.server:on("disconnect", function(data,client)
        userDB:logout(client.connectId)
        local room = self.clients[client.connectId]
        if room then
            self:leaveRoom(client,room)
            print("client:"..client.connectId.." left room")
        else
            print("client:"..client.connectId.." disconnected")
        end
        
    end)
    
    self.server:on("ping",function(_,client)
        client:send("pong")
    end)

    self.server:on("register",function(data,client)
        local d = userDB:checkAndReg(data,client.connectId)
        if d then
            client:send("register_ok",d)
        else
            client:send("register_err")
        end
    end)

    self.server:on("login",function(data,client)
        local d = userDB:login(data,client.connectId)
        if d then
            client:send("login_ok",d)
        else
            client:send("login_err")
        end
    end)

    self.server:on("create_room",function(data,client)
        local room = self:createRoom(client,data)
        client:send("your_room",self:getRoomInfo(room))    
    end)

    self.server:on("join_room",function(data,client)
        local room = self.rooms[data.id]
        self:joinRoom(client,room)
    end)

    self.server:on("leave_room",function(data,client)
        local room = self.rooms[data.id] --可以被替换为roomID
        self:leaveRoom(client,room)
    end)


    self.server:on("need_room_info",function(data,client)
        local room = self.rooms[data.id]
        client:send("your_room",self:getRoomInfo(room)) 
    end)

    self.server:on("need_roomlist",function(_,client)
        local data = {}
        for id, room in pairs(self.rooms) do
            table.insert(data,self:getRoomInfo(room))
        end
        client:send("your_roomlist",data)
    end)
    

    self.server:on("need_init",function(data,client)
        local room = self.rooms[data.id]
        self:syncGame(room,client)
    end)

    self.server:on("need_fire",function(data,client)
        local room = self.clients[client.connectId]
        if not room then return end
        local bullet = Bullet(room.game,unpack(data))
        bullet:setID(room.game:newNetID())
        self:sendToOthers(room,client,"create_obj",bullet:getData())
    end)

    self.server:on("need_move",function(data,client)
        local room = self.clients[client.connectId]
        if not room then return end
        local game = room.game
        local obj = game.objects[data.id]
        if obj then
            obj.dirty = true
            obj.x,obj.y,obj.rot = data.x,data.y,data.rot
        end
    end)
end